﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frmChucVu : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến

        public string maChucVu = "";
        TextMessageBox box = new TextMessageBox();
        DataTable dt = new DataTable();
        ChucVu_BLL cv_bll = new ChucVu_BLL();
        private bool isOK = false;
        #endregion

        #region LoadForm
        public frmChucVu()
        {
            InitializeComponent();
        }

        private void frmChucVu_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Các chức năng
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }
        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region Các sự kiện hệ thống
        private void frmChucVu_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                ChucVu_Public cv = new ChucVu_Public();
                if (maChucVu == "")
                {
                    if (checkTenInsert())
                    {
                        cv.MaChucVu = box.AutoID("CV", 2, cv_bll.LayDuLieu());
                        cv.TenChucVu =box.toUpper(txtTenChucVu.Text);
                        cv.GhiChu = txtGhiChu.Text;

                        cv_bll.Insert(cv, cv, cv);
                        SetInterface();
                    }
                }
                else
                {
                    cv.MaChucVu = txtMaChucVu.Text.Trim();
                    cv.TenChucVu =box.toUpper(txtTenChucVu.Text);
                    cv.GhiChu = txtGhiChu.Text;

                    cv_bll.Update(cv, cv, cv);
                    SetInterface();
                }
            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maChucVu != "")
            {
                dt = cv_bll.TaoBang(box.sqlCheckExist("DM_ChucVu","MaChucVu",maChucVu));
                DataRow dr=dt.Rows[0];
                txtMaChucVu.Text = maChucVu;
                txtTenChucVu.Text = dr["TenChucVu"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private bool checkValid()
        {

            if (txtTenChucVu.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên chức vụ.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenChucVu.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = cv_bll.TaoBang(box.sqlCheckExist("DM_ChucVu","TenChucVu",txtTenChucVu.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenChucVu.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenChucVu.Focus();
                return false;
            }
            return true;
        }
        private void SetInterface()
        {
            txtMaChucVu.Text = "Mã tự sinh";
            txtTenChucVu.Text = "";
            txtGhiChu.Text = "";
            maChucVu = "";
            txtTenChucVu.Focus();
        }
        #endregion
    }
}